var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
let statusMap = {
  '1': "未付款",
  '2': "待发货",
  '3': "待收货",
  '4': "用户收货",
  '5': "完成交易",
  '6': "已付款团购",
  '-1': "用户取消",
  '-3': "系统取消",
  '-4': "已超时团购",
  '-5': "申请退款",
  '-6': "已退款"
}
Page({
  data: {
    orderList: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 1
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    let that = this
    let {source} = options
    this.source = source
    try {
      var tab = wx.getStorageSync('tab');

      this.setData({
        showType: tab
      });
    } catch (e) {}

  },
  getOrderList() {
    let that = this;
    util.request(api.OrderList, {
      orderStatus: that.data.showType,
      current: that.data.page,
      size: that.data.limit
    }, 'POST').then(function(res) {
      if (res.errno === 0) {
        console.log(res.data);
        let orderList = res.data.list
        orderList=orderList.map(res => {
          res.orderStatusText = statusMap[res.orderStatus]
          return res
        })
        that.setData({
          orderList: that.data.orderList.concat(orderList),
          totalPages: res.data.pages,

        });
      }
    });
  },
  clear(){
    this.source=undefined
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getOrderList();
    } else {
      wx.showToast({
        title: '没有更多订单了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  switchTab: function(event) {
    let showType = event.currentTarget.dataset.index;
    this.setData({
      orderList: [],
      showType: showType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getOrderList();
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    this.getOrderList();
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
    this.source == 'order' && wx.switchTab({
      url: '/pages/ucenter/index/index',
    })
  }
})